package net.lab0.nebula.gui;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JFormattedTextField;

import net.lab0.tools.swing.RegexFormatter;
import javax.swing.JCheckBox;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class MapPanel
extends JPanel
{
    private MainWindow          mainWindow;
    private JCheckBox           chckbxQuadTree;
    private MandelbrotComponent mandelbrotComponent;
    private JCheckBox chckbxMandelbrotSet;
    
    public MapPanel(MainWindow mainWindow)
    {
        this.mainWindow = mainWindow;
        
        setLayout(new BorderLayout(0, 0));
        
        mandelbrotComponent = new MandelbrotComponent(this.mainWindow);
        add(mandelbrotComponent, BorderLayout.CENTER);
        
        JPanel panel = new JPanel();
        add(panel, BorderLayout.WEST);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[] { 52, 103, 0 };
        gbl_panel.rowHeights = new int[] { 16, 0, 0, 0 };
        gbl_panel.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
        gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
        panel.setLayout(gbl_panel);
        
        JLabel lblIterations = new JLabel("Iterations");
        GridBagConstraints gbc_lblIterations = new GridBagConstraints();
        gbc_lblIterations.insets = new Insets(0, 0, 5, 5);
        gbc_lblIterations.anchor = GridBagConstraints.NORTHEAST;
        gbc_lblIterations.gridx = 0;
        gbc_lblIterations.gridy = 0;
        panel.add(lblIterations, gbc_lblIterations);
        
        JFormattedTextField formattedTextField = new JFormattedTextField(new RegexFormatter("[1-9][0-9]{0,5}"));
        formattedTextField.setText("" + mandelbrotComponent.getFractal().getMaxIteration());
        GridBagConstraints gbc_formattedTextField = new GridBagConstraints();
        gbc_formattedTextField.insets = new Insets(0, 0, 5, 0);
        gbc_formattedTextField.fill = GridBagConstraints.HORIZONTAL;
        gbc_formattedTextField.anchor = GridBagConstraints.NORTH;
        gbc_formattedTextField.gridx = 1;
        gbc_formattedTextField.gridy = 0;
        panel.add(formattedTextField, gbc_formattedTextField);
        
        chckbxQuadTree = new JCheckBox("Quad tree");
        chckbxQuadTree.setSelected(true);
        chckbxQuadTree.addChangeListener(new ChangeListener()
        {
            public void stateChanged(ChangeEvent e)
            {
                mandelbrotComponent.setMandelbrotQuadTreeDisplayed(chckbxQuadTree.isSelected());
                mandelbrotComponent.repaint();
            }
        });
        
        chckbxMandelbrotSet = new JCheckBox("Mandelbrot set");
        chckbxMandelbrotSet.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mandelbrotComponent.setMandelbrotSetDisplayed(chckbxMandelbrotSet.isSelected());
            }
        });
        GridBagConstraints gbc_chckbxMandelbrotSet = new GridBagConstraints();
        gbc_chckbxMandelbrotSet.anchor = GridBagConstraints.WEST;
        gbc_chckbxMandelbrotSet.gridwidth = 2;
        gbc_chckbxMandelbrotSet.insets = new Insets(0, 0, 5, 5);
        gbc_chckbxMandelbrotSet.gridx = 0;
        gbc_chckbxMandelbrotSet.gridy = 1;
        panel.add(chckbxMandelbrotSet, gbc_chckbxMandelbrotSet);
        GridBagConstraints gbc_chckbxQuadTree = new GridBagConstraints();
        gbc_chckbxQuadTree.anchor = GridBagConstraints.WEST;
        gbc_chckbxQuadTree.gridwidth = 2;
        gbc_chckbxQuadTree.insets = new Insets(0, 0, 0, 5);
        gbc_chckbxQuadTree.gridx = 0;
        gbc_chckbxQuadTree.gridy = 2;
        panel.add(chckbxQuadTree, gbc_chckbxQuadTree);
    }
}
